home *** CD-ROM | disk | FTP | other *** search
- /*
- * $RCSfile: initializeLocks.c,v $
- * $Revision: 1.1.1.1 $
- * $Date: 1996/05/04 21:55:51 $
- */
- /**********************************************************************
- * EXODUS Database Toolkit Software
- * Copyright (c) 1991 Computer Sciences Department, University of
- * Wisconsin -- Madison
- * All Rights Reserved.
- *
- * Permission to use, copy, modify and distribute this software and its
- * documentation is hereby granted, provided that both the copyright
- * notice and this permission notice appear in all copies of the
- * software, derivative works or modified versions, and any portions
- * thereof, and that both notices appear in supporting documentation.
- *
- * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
- * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.
- * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
- * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
- *
- * The EXODUS Project Group requests users of this software to return
- * any improvements or extensions that they make to:
- *
- * EXODUS Project Group
- * c/o David J. DeWitt and Michael J. Carey
- * Computer Sciences Department
- * University of Wisconsin -- Madison
- * Madison, WI 53706
- *
- * or exodus@cs.wisc.edu
- *
- * In addition, the EXODUS Project Group requests that users grant the
- * Computer Sciences Department rights to redistribute these changes.
- **********************************************************************/
-
- #include "sysdefs.h"
- #include "ess.h"
- #include "checking.h"
- #include "trace.h"
- #include "error.h"
- #include "list.h"
- #include "pool.h"
- #include "tid.h"
- #include "io.h"
- #include "lock.h"
- #include "object.h"
- #include "msgdefs.h"
- #include "thread.h"
- #include "semaphore.h"
- #include "link.h"
- #include "lsn.h"
- #include "latch.h"
- #include "bf.h"
- #include "volume.h"
- #include "trans.h"
- #include "util_funcs.h"
- #include "lm_intfuncs.h"
- #include "lm_extfuncs.h"
- #include "lm_globals.h"
- #include "lock_globals.h"
- #include "thread_globals.h"
-
- static void
- initLockEntry(LOCKENTRY *lockEntry)
- {
- TRACE(TR_LOCK, TR_LEVEL_1);
- initializeListElement( &(lockEntry->transList), (char *) lockEntry );
- initializeListElement( &(lockEntry->ageList), (char *) lockEntry );
- initializeListElement( &(lockEntry->headerList.list), (char *) lockEntry );
- initializeList( &(lockEntry->threadList) );
- INIT_LOCKENTRY_MAGIC(lockEntry);
- /* the rest of the fields are initialized in newLockEntry(), because they
- * need to be REinitialized for re-use after they are put back in the Pool.
- */
- }
-
- LOCKENTRY
- *allocLockEntry (
- POOL *head
- )
- {
- LOCKENTRY *lockEntry;
-
- TRACE(TR_LOCK, TR_LEVEL_1);
- if ((lockEntry = (LOCKENTRY *) malloc(sizeof(LOCKENTRY))) == NULL) {
- SM_ERROR(TYPE_FATAL, esmMALLOCFAILED);
- }
- initLockEntry(lockEntry);
- head->elements++;
- return(lockEntry);
- }
-
- void
- initializeLocks ()
- {
- LIST *lockList;
- LOCKHEADER *lockHeader;
- LOCKENTRY *lockEntry;
- LOCKCLASSREC *lockClass;
- UFOUR spaceNeeded;
- FOUR i;
-
-
- TRACE(TR_LOCK, TR_LEVEL_1);
-
- /*
- * LOCK TABLE
- */
-
- if (!checkLog2(LockTableSize)) {
- /* LockTableSize must be a power of two */
- SM_ERROR(TYPE_FATAL, esmBADLOCKTABLESIZE);
- } else {
- LockHashMask = LockTableSize - 1;
- }
- TRPRINT(TR_LOCK, TR_LEVEL_2, ("LockTableSize:%d LockHashMask:%x",
- LockTableSize, LockHashMask));
- spaceNeeded = sizeof(LIST) * LockTableSize;
- if ((lockList = (LIST *) malloc(spaceNeeded)) == NULL) {
- SM_ERROR(TYPE_FATAL, esmMALLOCFAILED);
- }
- LockHashTable = lockList;
- for (i = 0; i < LockTableSize; i++, lockList++) {
- initializeList(lockList);
- }
-
- /*
- * LOCK HEADER POOL
- */
- initializePool( &LockHeaderPool, (ALLOCFUNC) allocLockHeader, NumLockHeaders );
- spaceNeeded = sizeof(LOCKHEADER) * NumLockHeaders;
- if ((lockHeader = (LOCKHEADER *) malloc(spaceNeeded)) == NULL) {
- SM_ERROR(TYPE_FATAL, esmMALLOCFAILED);
- }
- for (i = 0; i < NumLockHeaders; i++, lockHeader++) {
- initializeListElement( &(lockHeader->hashList.list), (char *) lockHeader );
- listEnq( &(LockHeaderPool.freeList), &(lockHeader->hashList.list) );
- initializeList( &(lockHeader->grantedList) );
- initializeList( &(lockHeader->waitList) );
- initializeList( &(lockHeader->upgradeList) );
- initializeList( &(lockHeader->auxList) );
- INIT_LOCKHEADER_MAGIC(lockHeader);
- }
-
- /*
- * LOCK ENTRY POOL
- */
- initializePool( &LockEntryPool, (ALLOCFUNC) allocLockEntry, NumLockEntries );
- spaceNeeded = sizeof(LOCKENTRY) * NumLockEntries;
- if ((lockEntry = (LOCKENTRY *) malloc(spaceNeeded)) == NULL) {
- SM_ERROR(TYPE_FATAL, esmMALLOCFAILED);
- }
- for (i = 0; i < NumLockEntries; i++, lockEntry++) {
- initLockEntry(lockEntry);
- listEnq( &(LockEntryPool.freeList), &(lockEntry->transList) );
- }
-
- /*
- * LOCK CLASS POOL
- */
- initializePool( &LockClassPool, (ALLOCFUNC) allocLockClassRec, NumLockClassRecs );
- spaceNeeded = sizeof(LOCKCLASSREC) * NumLockClassRecs;
- if ((lockClass = (LOCKCLASSREC *) malloc(spaceNeeded)) == NULL) {
- SM_ERROR(TYPE_FATAL, esmMALLOCFAILED);
- }
- for (i = 0; i < NumLockClassRecs; i++, lockClass++) {
- initializeListElement( &(lockClass->transList), (char *) lockClass );
- initializeListElement( &(lockClass->entryList.list), (char *) lockClass );
- listEnq( &(LockClassPool.freeList), &(lockClass->entryList.list) );
- INIT_LOCKCLASSREC_MAGIC(lockClass);
- }
- }
-
- LOCKENTRY
- *newLockEntry (
- register TRANSREC *transRec,
- LOCKHEADER *lockHeader,
- int whichList,
- LOCKMODE requestMode
- )
- {
-
- register LOCKENTRY *lockEntry;
-
-
- TRACE(TR_LOCK, TR_LEVEL_1);
-
- /*
- * attempt to get a lock entry structure
- */
- if ((lockEntry = (LOCKENTRY *) poolDeq( &LockEntryPool )) == NULL) {
- SM_ERROR(TYPE_FATAL, esmNOFREELOCKENTRY);
- return(NULL);
- }
-
- lockEntry->lockHeader = lockHeader;
- lockEntry->headerList.transRec = transRec;
-
- switch (whichList) {
-
- case GRANT_LIST:
-
- listPush( &(lockHeader->grantedList), &(lockEntry->headerList.list) );
- listPush( &(transRec->lockGrantedList), &(lockEntry->transList) );
- lockEntry->flags = LOCK_GRANTED;
- break;
-
- case WAIT_LIST:
-
- listEnq( &(lockHeader->waitList), &(lockEntry->headerList.list) );
- listPush( &(transRec->lockWaitList), &(lockEntry->transList) );
- lockEntry->flags = LOCK_WAITER;
- break;
-
- case UPGRADE_LIST:
-
- listEnq( &(lockHeader->upgradeList), &(lockEntry->headerList.list) );
- listPush( &(transRec->lockUpgradeList), &(lockEntry->transList) );
- lockEntry->flags = LOCK_UPGRADE;
- break;
-
- default:
-
- SM_ERROR(TYPE_FATAL, esmINTERNAL);
- break;
- }
-
- lockEntry->lockMode = requestMode;
- lockEntry->time2wakeup = 0;
- lockEntry->weight = 0;
-
- /*
- * return a pointer to the lock entry
- */
- TRPRINT(TR_LOCK, TR_LEVEL_1,
- ("Thread %d, trans %ld, ALLOCating 0x%x mode=%s flag=%d",
- Active->id, Active->message.header.params.in.tid, lockEntry,
- GETMODE(lockEntry->lockMode), (int)lockEntry->flags));
- TRPRINT(TR_LOCK, TR_LEVEL_1,
- ("lock (id %s) entry for trans %d",
- printLockId(lockEntry->lockHeader->hashList.lockid),
- lockEntry->headerList.transRec->tid) );
-
- return(lockEntry);
- }
- void
- freeLockEntry (
- register LOCKENTRY *lockEntry,
- int error
- )
- {
- TRACE(TR_LOCK, TR_LEVEL_1);
-
- TRPRINT(TR_LOCK, TR_LEVEL_1,
- ("Thread %d, trans %ld, FREEing 0x%x mode=%s flag=%d",
- Active->id, Active->message.header.params.in.tid, lockEntry,
- GETMODE(lockEntry->lockMode), (int)lockEntry->flags));
- TRPRINT(TR_LOCK, TR_LEVEL_1,
- ("lock (id %s) entry for trans %d",
- lockEntry->flags & LOCK_UPGRADE ?
- printLockId(((LOCKENTRY*)lockEntry->lockHeader)->lockHeader->hashList.lockid) :
- printLockId(lockEntry->lockHeader->hashList.lockid),
- lockEntry->headerList.transRec->tid) );
-
- lockEntry->headerList.transRec->lockWeight -= lockEntry->weight;
-
- /*
- * take it off the list of lockEntries to be aged
- */
- if(LIST_MEMBER (&(lockEntry->ageList)) ) {
- listRemove(&(lockEntry->ageList));
- lockEntry->time2wakeup = 0;
- } else {
- SM_ASSERT(LEVEL_1, (lockEntry->time2wakeup == 0));
- }
- if (LIST_NOT_EMPTY( &(lockEntry->threadList) )) {
-
- notify( &(lockEntry->threadList),
- ((error == esmNOERROR)? esmNOERROR: esmFAILURE), error );
- }
-
- listRemove( &(lockEntry->headerList.list) );
- poolMove( &LockEntryPool, &(lockEntry->transList) );
- }
-